package demo3;

import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        char[][] chs = new char[n+1][n+1];
        for(int i = 1;i<=n;i++){
            String val = scanner.next();
            for(int j = 1;j<=n;j++){
                chs[i][j] = val.charAt(j-1);
            }
        }
        while(m-->0){
            int x1 = scanner.nextInt()+1;
            int y1 = scanner.nextInt()+1;
            int x2 = scanner.nextInt()+1;
            int y2 = scanner.nextInt()+1;
            int count = 0;
            for(int i = x1;i<=x2;i++){
                for(int j = y1;j<=y2;j++){
                    int x = (i-1)%n+1;
                    int y = (j-1)%n+1;
                    //System.out.println(x+" "+y);
                    if(chs[x][y]=='B') count++;
                }
            }
            System.out.println(count);
        }
    }
}
